package com.tmzn.openfeign;

import feign.Feign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;

import java.util.HashMap;
import java.util.Map;

/**
 * 缓存
 */
public class FeginClientFactory {
	
	/**
	 * 缓存所有的Fegin客户端
	 */
	private volatile static Map<String, Object> feginClientCache = new HashMap<>();
	
	/**
	 * 从Map中获取数据
	 * @return 
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getFeginClient(Class<T> clazz, String baseUrl){
		if(!feginClientCache.containsKey(baseUrl)) {
			synchronized (FeginClientFactory.class) {
				if(!feginClientCache.containsKey(baseUrl)) {
					T feginClient = Feign.builder().decoder(new JacksonDecoder()).encoder(new JacksonEncoder()).target(clazz, baseUrl);
					feginClientCache.put(baseUrl, feginClient);
				}
			}
		}
		return (T)feginClientCache.get(baseUrl);
	}
}
